/*******************************************************************************
 * Copyright (c) 2010, 2030 www.itlaobing.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.dao;/**
 * Created by asus-pc on 2017/11/15 0015.
 */

import cn.itlaobing.entity.Users;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * ClassName: UserDao <br/>
 * Function: TODO 功能描述 <br/>
 * date: 2017/11/15 0015 16:30 <br/>
 *
 * @author 卖姑娘的小柴火
 * @since JDK 1.8
 */
public class UserDao extends BaseDao {
    /**
     * 登录
     * @param username
     * @param userpwd
     * @return
     * @throws SQLExceptio
     */
    public Users login(String username,String userpwd) throws SQLException{
        String sql="select * from users where userAccount=? and userPassword=?";
        QueryRunner qr=new QueryRunner(getDataSource());
        BeanHandler<Users> beanHandler=new BeanHandler<Users>(Users.class);
        return qr.query(sql,beanHandler,username,userpwd);
    }

    /**
     * 注册
     * @param users
     * @return
     * @throws SQLException
     */
    public int register(Users users)throws SQLException{
        String sql="INSERT INTO itlaobing_eshop.users (userAccount, userPassword, userRegDateTime)" +
                "VALUES(?,?,?)";
        int r=0;
        QueryRunner qr=new QueryRunner(getDataSource());
        BeanHandler beanHandler=new BeanHandler(Users.class);
        r= (int) qr.insert(sql,beanHandler,users.getUseraccount(),users.getUserpassword(),users.getUserregdatetime());
        sql="INSERT INTO itlaobing_eshop.userinfo (userName, userPass, reg_date)" +
                "VALUES(?,?,?)";
        r= (int) qr.insert(sql,beanHandler,users.getUseraccount(),users.getUserpassword(),users.getUserregdatetime());
        return r;
    }

    public int setLastLoginTime()throws SQLException{
        String sql="INSERT INTO itlaobing_eshop.users userLastLoginDateTime VALUES NOW()";
        int r=0;
        QueryRunner qr=new QueryRunner(getDataSource());
        BeanHandler beanHandler=new BeanHandler(Users.class);
        r= (int) qr.insert(sql,beanHandler);
        return 0;
    }
//    public int setLoginCount()throws SQLException{
//        String sql="INSERT INTO itlaobing_eshop.users userLoginCount VALUES NOW()";
//    }
}
